﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using System.Collections;
using System.Data.SqlServerCe;

namespace EWS
{
    public partial class frmPatientView : Form
    {

        public uint myID;
        public string Vorname;
        public string Name;

        ArrayList myMeasureData;

        public frmPatientView()
        {
            InitializeComponent();
        }



        public ArrayList FUllData(uint id)
        {
            //// Verbinndung aufbauen
            SqlCeConnection conn = new SqlCeConnection(Properties.Settings.Default.EWS_DatabaseConnectionString);
            conn.Open();

            // Commando aufbauen
            SqlCeCommand cmd = conn.CreateCommand();
            cmd.CommandText = @"SELECT * FROM PatientData WHERE fkPaID=@id ORDER BY Timestamp";
            cmd.Parameters.AddWithValue("@id", id);

            cmd.ExecuteScalar();

            SqlCeDataReader reader = cmd.ExecuteReader();

            ArrayList PData = new ArrayList();
            while (reader.Read() != false)
            {
                PatientList.PatientData tmp = new PatientList.PatientData();
                tmp.ID = Convert.ToUInt32(reader.GetInt32(0));
                tmp.Atemfrequenz = Convert.ToUInt32(reader.GetString(6));
                tmp.Blutdruck = Convert.ToUInt32(reader.GetString(3));
                tmp.Puls = Convert.ToUInt32(reader.GetString(4));
                tmp.Sauerstoffsaettigung = Convert.ToUInt32(reader.GetString(5));
                tmp.Temperatur = float.Parse(reader.GetString(2));
                //MessageBox.Show(reader.GetInt32(7).ToString());
                tmp.timestamp = reader.GetString(8);

                PData.Add(tmp);
            }
            
            conn.Close();
            return PData;
        }


        void InsertDatas()
        {

            PatientList.Limit myLimit = new PatientList.Limit();
            //Series series1 = new Series(); // Für Temperatur

            double[] yValues1 = new double[myMeasureData.Count];
            uint[] yValues2 = new uint[myMeasureData.Count];
            uint[] yValues3 = new uint[myMeasureData.Count];
            uint[] yValues4 = new uint[myMeasureData.Count];
            uint[] yValues5 = new uint[myMeasureData.Count];
            uint[] yValuesResult = new uint[myMeasureData.Count];

            string[] xValuesTimestamp = new string[myMeasureData.Count];

            for (int i = 0 ; i < myMeasureData.Count; i++) {
                PatientList.PatientData dsOneDataset = (PatientList.PatientData)myMeasureData[i];

                if (i == 0) lblTime1.Text = dsOneDataset.timestamp;
                if (i == (myMeasureData.Count-1)) lblTime2.Text = dsOneDataset.timestamp;

                yValues1[i] = Math.Round(dsOneDataset.Temperatur,2);
                yValues2[i] = dsOneDataset.Blutdruck;
                yValues3[i] = dsOneDataset.Puls;
                yValues4[i] = dsOneDataset.Sauerstoffsaettigung;
                yValues5[i] = dsOneDataset.Atemfrequenz;

                xValuesTimestamp[i] = dsOneDataset.timestamp;
                yValuesResult[i] = myLimit.getLimitLevel(dsOneDataset);
                
            }

            chart1.Series["SeriesTemperatur"].Points.DataBindY(yValues1);
            chart1.Series["SeriesBlutdruck"].Points.DataBindY(yValues2);
            chart1.Series["SeriesPuls"].Points.DataBindY(yValues3);
            chart1.Series["SeriesSauerstoff"].Points.DataBindY(yValues4);
            chart1.Series["SeriesAtemfrequenz"].Points.DataBindY(yValues5);
            chart1.Series["SeriesResult"].Points.DataBindXY(xValuesTimestamp, yValuesResult);
        }


        private void frmpv_Load(object sender, EventArgs e)
        {
            this.Text = "TotalCare - " + Name +" "+ Vorname;
            lblName.Text = Name;
            lblVorname.Text = Vorname;

            myMeasureData = FUllData(myID); // Alle Messdaten aus Datenbank ermiteln
            InsertDatas();                  // In Diagramm einfügen

        }
    }
}

